//app.js
App({

  getSystemInfo: function () {
    let t = this;
    wx.getSystemInfo({
      success: function (res) {
        t.globalData.systemInfo = res;
      }
    });
  },

  onLaunch: function () {

    this.getSystemInfo();

    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // wx.api_host = 'http://wq-xshop.cloud.com';
    // 登录
    var t = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId

        var openid = wx.getStorageSync( 'openid' );
        var session_key = wx.getStorageSync( 'session_key' );

        if( typeof openid != 'undefined' || typeof session_key != 'undefined' ){

          wx.request({
            url: t.globalData.api_host + t.globalData.api_path+'&r=member.xcx.login',

            data:{
              code:res.code,
              i:t.globalData.i
            },
            header:{
              'content-type':'application/x-www-form-urlencoded'
              // 'content-type': 'application/json' // 默认值
            },
            success (res) {
              console.log(res.data);
              //todo : 存储 openid 和 sessionKey 到本地
              if( res.data.openid ){

                wx.setStorageSync( 'openid', res.data.openid );
                wx.setStorageSync( 'session_key', res.data.session_key );
                t.globalData.shop_name = res.data.shop_name?res.data.shop_name:t.globalData.shop_name;
              }
            }
          });

        }

      }
    });

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
/*  globalData: {
    appid: "wx85aca1163a609063",
    shop_name:'虾米商城',
    userInfo: null,
    api_host:'http://wq-xshop.cloud.com',
    // api_host:'https://www.x-mii.com',
    api_path:'/addons/xmi_shop/xcx.php?c=entry&a=site&m=xmi_shop&do=api',
    i:3,
    location_key : '254BZ-QQUKX-H474D-7KR67-P36L7-B6FQA' //使用在腾讯位置服务申请的key
  },*/

  globalData: {
    appid: "wx7eeaa62eb15e7233",
    shop_name:'顺佳鸿智能',
    userInfo: null,
    api_host:'https://wq.shunjiahong.com',
    api_path:'/addons/sm_shop/xcx.php?c=entry&a=site&m=sm_shop&do=api',
    i:1,
    location_key : 'NSCBZ-LJX3X-U3G4G-THZGP-ONWSZ-FSF6G' //使用在腾讯位置服务申请的key
  },


  request:function( url, data, method, callback ){

    var url = this.globalData.api_host + url;

    // var session_key = wx.getStorageSync('session_key');
    var openid = wx.getStorageSync('openid');

    url += '&i=' + this.globalData.i;
    url += '&openid=' + openid;
    var data_params = {
    //   i: this.globalData.i,
    //   openid:openid
    };
    var data_params_post = '';
    for(var p in data ){

      data_params[p] = data[p];
      data_params_post += '&' + p + '=' +data[p];
    }

    data_params_post = data_params_post.replace(/^&/,'');

    console.log( 'api request:' );
    console.log( url );
    wx.request({

      url: url,
      // data:method == 'POST' ? data_params_post : data_params,
      data: data_params,

      method:method,

      header:{
        'content-type':'application/x-www-form-urlencoded'
      },

      success:res=>{

        console.log( 'api response:' );
        console.log( res );
        callback( res );
      }

    });
  },

    get:function( url, data, callback ){
        url = url.indexOf('r=')<0 ? 'r='+url : url;
        url = url.indexOf('&')!=0? '&'+url : url;

      url = this.globalData.api_path + url;
      this.request( url, data, 'GET', callback );

    },

  getCustomer:function( callback ){

    var url = this.globalData.api_path + '&r=member.xcx.getCustomer';

    this.request( url, {}, 'GET', callback );

  },

  checkUserStatus:function( ){
    var url = this.globalData.api_path + '&r=member.xcx.getCustomer';

    this.request( url, {}, 'GET', res => {
      if( !res.data.telephone ){
        wx.navigateTo({
          url:'/pages/user/login'
        });
      }
    } );
  }
})